<template>
	<view class="container">
		<template v-if="drillList.length > 0">
			<block v-for="(item,index) in drillList" :key="index">
				<view class="drill-item white-bg z-b-r u-margin-bottom-20 u-padding-20 z-font-lg u-flex">
					<!-- <view class="u-margin-right-10">
						<u-tag :text="item.drill_status" shape="circle" :type="item.drill_status === '钻机使用中' ? 'warning' : 'primary'" />
					</view> -->
					<view class="u-flex-1">
						<view>
							<text>钻机编号:</text><text class="u-margin-left-10">{{ item.drill_id }}</text>
							<u-tag size="mini" :text="item.drill_status" shape="circle" :type="item.drill_status === '钻机使用中' ? 'warning' : 'primary'" />
						</view>
						<view>
							<text>购买时间:</text><text class="u-margin-left-20">{{ item.drill_buy_date }}</text>
						</view>
					</view>
					<view class="u-margin-top-20 u-text-right">
						<!-- <u-tag v-if="allotDrills.indexOf(item.drill_id) !== -1" @click="delCurProjectDrill(item.drill_id)" type="error" text="删除钻机"/> -->
						<u-tag :text="allotDrills.indexOf(item.drill_id) !== -1 ? '取消选择' : '点击选择'" 
						:type="allotDrills.indexOf(item.drill_id) !== -1 ? 'success' : 'info'" @click="selectDrillHandle(index)"/>
					</view>
				</view>
			</block>
		</template>
		<template v-else>
			<empty-data></empty-data>
		</template>
		<!-- 底部导航 -->
		<bottom-bar>
			<u-button :disabled="disabledBtn" type="primary" @click="confirmSelectDrill">提交</u-button>
		</bottom-bar>
		<!-- 消息提示 -->
		<u-toast ref="uToast" />
		<u-top-tips ref="uTips"></u-top-tips>
		<!-- 模态框 -->
		<u-modal v-model="showModal" show-cancel-button="true" @cancel="cancelHandle" @confirm="confirmHandle" content="钻机正在使用中,是否确认分配,如果确认系统将删除另一个项目的钻机!"></u-modal>
		<u-modal v-model="showModalUpdate" show-cancel-button="true" @cancel="cancelHandleUpdate" @confirm="confirmHandleUpdate" content="当前项目已经分配钻机,是否确认修改？"></u-modal>
	</view>
</template>

<script>
	import {mapState} from "vuex";
	export default {
		data() {
			return {
				drillList:[],
				curDrillIndex:-1,
				showModal:false,
				disabledBtn:false,
				curProjectID:"",
				curDrillID:"",
				showModalUpdate:false,
				allotDrills:[]  //分配钻机
				
			}
		},
		computed:{
			...mapState({
				token:"token",
				pm_company:"pm_company"
			})
		},
		onLoad(params) {
			if(params.drillID) this.allotDrills = params.drillID.split(",");
			this.curProjectID = params.projectID;
			this.getDrillInfo();
		},
		methods: {
			selectDrillHandle(i){
				let {drillList,allotDrills} = this;
				this.curDrillID = drillList[i].drill_id;
				//判断是否已经分配
				if(drillList[i].drill_status == "钻机使用中" && (allotDrills.indexOf(drillList[i].drill_id) == -1)) this.showModal = true;
				// 删除本项目的该钻机
				if(drillList[i].drill_status == "钻机使用中" && (allotDrills.indexOf(drillList[i].drill_id) !== -1)){
					this.delCurProjectDrill(this.curDrillID)
					return false;
				}
				// 第二次点击时取消分配
				if(allotDrills.indexOf(this.curDrillID) !== -1){
					this.allotDrills = allotDrills.filter(id=>id !== this.curDrillID)
				} else {
					this.allotDrills.push(this.curDrillID);
				}
			},
			cancelHandleUpdate(){},
			confirmHandleUpdate(){
				this.updateDrillStatus();
				this.sendSelectDrill();
			},
			cancelHandle(){
				let {allotDrills,curDrillID} = this;
				this.allotDrills = allotDrills.filter(id=>id !== curDrillID);
			},
			async confirmHandle(){ //分配已经分配的钻机
				//确认分配删除前一个项目中的该钻机
				let {drillList,curDrillIndex,curDrillID} = this;
				this.$loading("正在处理");
				let res = await this.$network({
						api:"main",
						data:{
							action:"drillAdmin/deleteDrillInProject",
							data:{
								drillID:curDrillID
							}
						}
					})

				uni.hideLoading();
				if(res.status === 0){
					this.updateDrillStatus();
				} else {
					this.$refs.uTips.show({
						title: '稍后重试',
						type: 'warning'
					})
				}
			},
			// 更改数据库钻机状态
			async updateDrillStatus(){
				this.$loading("正在处理");
				let {drillList,curDrillID} = this;
				let res = await this.$network({
					api:"main",
					data:{
						action:"drillAdmin/updateDrillStatus",
						data:{
							curDrillStatus:'未分配使用',
							drillID:curDrillID
						}
					}
				})
				
				uni.hideLoading()
				if(res.status === 0){
					this.drillList.forEach((item,index)=>{
						if(item.drill_id === curDrillID){
							this.drillList[index].drill_status = '未分配使用';
						}
					})
				} else {
					this.$refs.uTips.show({
						title: '稍后重试',
						type: 'warning'
					})
				}
			},
			confirmSelectDrill(){
				let {allotDrills,drillList,curDrillIndex,curDrillID} = this;
				if(allotDrills.length <= 0){
					this.$refs.uTips.show({
						title: '请先选择钻机',
						type: 'warning'
					})
				} else {
					this.sendSelectDrill();
				}
			},
			// 发送分配请求
			async sendSelectDrill(){
				let {drillList,curDrillIndex,curDrillID,allotDrills} = this;
				//1、 给当前项目添加钻机
				//2、修改钻机状态
				//3、 如果是已经分配的钻机还需要删除其他项目的钻机
				this.$loading("正在分配")
				this.disabledBtn = true;
				
				 //分配 钻机
				let allotDrillRes = await this.$network({
					api:"main",
					data:{
						action: 'drillAdmin/allotDrillInProject',
						data:{
							projectID:this.curProjectID,
							drillID:allotDrills
						}
					}
				})
				
				if(allotDrillRes.status !== 0) return false;
				
				// 更改钻机状态
				let updateDrillStatusTasks = [];
				allotDrills.forEach(drillID=>{
					updateDrillStatusTasks.push(this.$network({
						api:"main",
						data:{
							action:"drillAdmin/updateDrillStatus",
							data:{
								curDrillStatus:'钻机使用中',
								drillID
							}
						}
					}))
				})
				
				let updateDrillStatusRes = await Promise.all(updateDrillStatusTasks);
				// console.log(updateDrillStatusRes);
				uni.hideLoading();
				this.disabledBtn = false;
				this.$refs.uTips.show({
					title: '分配成功,返回刷新',
					type: 'success'
				})
			},
			async getDrillInfo(){
				let {token,pm_company} = this;
				this.$loading("正在加载");
				
				let res = await this.$network({
					api:"main",
					data:{
						action:"disposeData/getDrillInfoByPmCompany",
						data:{token,pm_company}
					}
				})
				
				if(res.status === 0){
					uni.hideLoading();
					this.drillList = res.data;
				} else {
					uni.hideLoading();
					this.$refs.uTips.show({
						title: '请先添加钻机',
						type: 'error'
					})
					
				}
			},
			// 删除本项目钻机
			async delCurProjectDrill(drillID){
				let {allotDrills,drillList} = this,
					delAllots = allotDrills.filter(id=>id!==drillID);
				this.allotDrills = delAllots;
				this.$loading("正在移除")
				//重新 分配 钻机
				let allotDrillRes = await this.$network({
					api:"main",
					data:{
						action: 'drillAdmin/allotDrillInProject',
						data:{
							projectID:this.curProjectID,
							drillID:delAllots
						}
					}
				})
				
				
				//更改 钻机 状态
				let updateDrillStatus = await this.$network({
					api:"main",
					data:{
						action:"drillAdmin/updateDrillStatus",
						data:{
							curDrillStatus:'未分配使用',
							drillID
						}
					}
				})
				
				uni.hideLoading();
				if(allotDrillRes.status === 0 && updateDrillStatus.status === 0){
					this.drillList.forEach((item,index)=>{
						if(item.drill_id === drillID){
							this.drillList[index].drill_status = "未分配使用"
						}
					})
					this.$refs.uTips.show({
						title: '删除成功',
						type: 'success'
					})
				}
				
			}
		}
	}
</script>
